#include "GAF_Face.h"

namespace GAF
{
	Face::Face()
	{
		m_nGType	= GRAPHICS_FACE;
		m_pimgFace	= NULL;
	}

	Face::Face(unsigned int nWidth, unsigned int nHeight)
	{
		GetSize().nWidth = nWidth;
		GetSize().nHeight = nHeight;
		m_pimgFace = Image::Create(nWidth, nHeight);
	}

	Face::~Face()
	{
		if(m_pimgFace != NULL)
			Image::Destroy(m_pimgFace);

		m_lnkGraphicses.Clean();
	}

	bool Face::Init(Bundle* pParam)
	{
		return OnInit(pParam);
	}

	void Face::AddGraphics(Graphics* pGraphics)
	{
		GAF_CHECK(pGraphics != NULL);

		if(pGraphics->GetDockGroup() == this)
			RemoveGraphics(pGraphics);

		m_lnkGraphicses.Add(pGraphics);
		pGraphics->DockGroup(this);
	}

 	void Face::AddGraphicses(Graphics** pGraphics, COUNT nCount)
 	{
 		for(UINT _i = 0; _i < nCount; _i++)
 			AddGraphics(pGraphics[_i]);
 	}

	void Face::RemoveGraphics(Graphics* pGraphics)
	{
		GAF_CHECK(pGraphics != NULL);

		m_lnkGraphicses.Remove(pGraphics);
		pGraphics->DockGroup(NULL);
	}

	void Face::CleanGraphics()
	{
		int _nLnkCount = m_lnkGraphicses.GetCount();
		if(_nLnkCount > 0)
		{
			Graphics** _pGraphics = GAF_NEW_ARY(Graphics*, _nLnkCount);
			m_lnkGraphicses.Clean(_pGraphics);

			for(int _i = 0; _i < _nLnkCount; _i++)
			{
				_pGraphics[_i]->DockGroup(NULL);
			}

			GAF_DELETE(_pGraphics);
		}
	}

	Pointer Face::Foreach(bool isFirst)
	{
		static Graphics** s_ppGraphics;
		s_ppGraphics = (Graphics**)m_lnkGraphicses.Foreach(isFirst);

		if(s_ppGraphics == NULL)
			return NULL;
		else
			return (Pointer)*s_ppGraphics;
	}

	UINT Face::GetCount()
	{
		return m_lnkGraphicses.GetCount();
	}

		void Face::RefreshSize()
	{
		if(IsIgnoreSize())
			return;

		Rect _rArea1 = {0, 0, 0, 0};
		Rect _rArea2 = {0, 0, 0, 0};


		FOREACH(Graphics*, _ppG, m_lnkGraphicses)
		{
			if((*_ppG)->IsIgnoreSize() == false)
			{
				if((*_ppG)->GetGType() == GRAPHICS_FACE)
					((Face*)*_ppG)->RefreshSize();

				_rArea2.Set(GAF_ARG_POINT((*_ppG)->GetLocation()), GAF_ARG_SIZE((*_ppG)->GetSize()));
				_rArea1 = _rArea1.Compose(_rArea2);
			}
		}

		SetSize(_rArea1.nWidth, _rArea1.nHeight);
	}
}
